using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.CommonWeb;
using System.IO;

public partial class admin_webparts_Plug_ins_Files_Explorer : System.Web.UI.UserControl
{
    public static string ROOT_PATH = "../" + Constant.UPLOAD_FILE_PATH + "Files/";
    public static string FILES_MESSAGE = "FILES_MESSAGE";
    public static string FILES_MESSAGE_TYPE = "FILES_MESSAGE_TYPE";

    public long _TotalItem = 0;
    public string _CurrentPath = "/";
    public string[] _FileList = new string[0];
    public string[] _DirectoryList = new string[0];
    public string _Message = string.Empty;
    public int _MessageType = 0; // 0: info, -1: error.

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (this.GetCookie(FILES_MESSAGE) != null)
            {
                _Message = this.GetCookie(FILES_MESSAGE).ToString();
                this.DeleteCookie(FILES_MESSAGE);
                _MessageType = int.Parse(this.GetCookie(FILES_MESSAGE_TYPE));
                this.DeleteCookie(FILES_MESSAGE_TYPE);
            }

            if (Request.QueryString["file"] != null)
            {
                _CurrentPath = Request.QueryString["file"];
            }

            string rootServerPath = Server.MapPath(ROOT_PATH);
            string currentServerPath = Path.Combine(Server.MapPath(ROOT_PATH), this._CurrentPath.TrimStart('/').Replace("/", "\\"));

            if (!Directory.Exists(Server.MapPath(ROOT_PATH)))
            {
                Directory.CreateDirectory(Server.MapPath(ROOT_PATH));
            }

            if (!Directory.Exists(currentServerPath))
            {
                Response.Redirect("?type=page&name=files", false);
                return;
            }

            _DirectoryList = Directory.GetDirectories(currentServerPath);
            if (_DirectoryList != null)
            {
                for (int i = 0; i < _DirectoryList.Length; i++)
                {
                    _DirectoryList[i] = Path.GetFileName(_DirectoryList[i]);
                }
            }
            _FileList = Directory.GetFiles(currentServerPath);
            if (_FileList != null)
            {
                for (int i = 0; i < _FileList.Length; i++)
                {
                    _FileList[i] = Path.GetFileName(_FileList[i]);
                }
            }
            _TotalItem = _DirectoryList.Length + _FileList.Length;
        }
        catch (Exception ex)
        {
            _Message = ex.Message + "<br />" + ex.StackTrace;
            _MessageType = -1; // error
        }
    }

    public string GetCookie(string name)
    {
        //return CookieUtility.GetCookieValue(name, Request);
        return Session[name] == null ? null : Session[name].ToString();
    }

    public void DeleteCookie(string name)
    {
        Session.Remove(name);
        //CookieUtility.DeleteCookie(name, Request, Response);
    }

    public string ParentPath
    {
        get
        {
            if (_CurrentPath == "/")
            {
                return null;
            }

            string parentPath = _CurrentPath.Substring(0, _CurrentPath.Length - 1);
            parentPath = parentPath.Substring(0, parentPath.LastIndexOf("/") + 1);

            return parentPath;
        }
    }
}
